package edu.ucsd.cse110;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import edu.ucsd.cse110.model.Lift;

/**
 * Displays a list of lifts.
 */
public class LiftActivity extends ListActivity {
	private class LiftsAdapter extends ArrayAdapter<Lift> {
		private Activity mActivity;

		/**
		 * Constructor.
		 */
		public LiftsAdapter(Context context, int textViewResourceId,
				List<Lift> objects) {
			super(context, textViewResourceId, objects);

			mActivity = (Activity) context;
			mLifts = objects;
		}

		/**
		 * TODO: Need documentation.
		 */
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) mActivity
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			final View view = inflater.inflate(R.layout.lifts_item, parent,
					false);

			// Shows the details of the WOD when the item is pressed.
			view.setTag(position);
			view.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					int position = (Integer) v.getTag();
					Lift lift = mLifts.get(position);

					Intent i = new Intent(mActivity, LiftDetailActivity.class);

					i.putExtra("Name", lift.getName());
					i.putExtra("LiftId", lift.getLiftId());
					i.putExtra("Setup", lift.getSetup());
					i.putExtra("Execution", lift.getExecution());
					i.putExtra("Picture", lift.getPicture());
					i.putExtra("DateOne", lift.getDateOne().getTime());
					i.putExtra("DateThree", lift.getDateThree().getTime());
					i.putExtra("DateFive", lift.getDateFive().getTime());
					i.putExtra("RepOne", lift.getRepOne());
					i.putExtra("RepThree", lift.getRepThree());
					i.putExtra("RepFive", lift.getRepFive());

					mActivity.startActivityForResult(i, REQUEST_CODE_DETAIL);
				}
			});

			// Sets the name of the WOD
			TextView txtName = (TextView) view
					.findViewById(R.id.lifts_txt_title);
			txtName.setText(mLifts.get(position).getName());

			// Sets the current One Rep value recorded
			TextView txtRepOne = (TextView) view
					.findViewById(R.id.lifts_txt_recordone);
			txtRepOne.setText(Integer
					.toString(mLifts.get(position).getRepOne()));

			// Sets the current Three Rep value recorded
			TextView txtRepThree = (TextView) view
					.findViewById(R.id.lifts_txt_recordthree);
			txtRepThree.setText(Integer.toString(mLifts.get(position)
					.getRepThree()));

			// Sets the current Five Rep value recorded
			TextView txtRepFive = (TextView) view
					.findViewById(R.id.lifts_txt_recordfive);
			txtRepFive.setText(Integer.toString(mLifts.get(position)
					.getRepFive()));

			return view;
		}
	}

	private LiftsAdapter mAdapter;
	private LocalDatabaseManager mDatabase;
	private List<Lift> mLifts;
	private final int REQUEST_CODE_DETAIL = 1;

	/**
	 * @param list
	 *            the List of lifts recorded for the given lift ID.
	 * @return returns the date of the most recently recorded Five Rep lift.
	 */
	private Date getLatestDateFive(List<Lift> list) {
		Date date;

		for (int i = list.size() - 1; i >= 0; i--) {
			date = list.get(i).getDateFive();
			if (date.getTime() != 0) {
				return date;
			}
		}

		return new Date(0);
	}

	/**
	 * @param list
	 *            the List of lifts recorded for the given lift ID.
	 * @return returns the date of the most recently recorded One Rep lift.
	 */
	private Date getLatestDateOne(List<Lift> list) {
		Date date;

		for (int i = list.size() - 1; i >= 0; i--) {
			date = list.get(i).getDateOne();
			if (date.getTime() != 0) {
				return date;
			}
		}

		return new Date(0);
	}

	/**
	 * @param list
	 *            the List of lifts recorded for the given lift ID.
	 * @return returns the date of the most recently recorded Three Rep lift.
	 */
	private Date getLatestDateThree(List<Lift> list) {
		Date date;

		for (int i = list.size() - 1; i >= 0; i--) {
			date = list.get(i).getDateThree();
			if (date.getTime() != 0) {
				return date;
			}
		}

		return new Date(0);
	}

	/**
	 * @param list
	 *            the List of lifts recorded for the given lift ID.
	 * @return returns the most recently recorded Five Rep value.
	 */
	private int getLatestRepFive(List<Lift> list) {
		for (int i = list.size() - 1; i >= 0; i--) {
			int rep = list.get(i).getRepFive();

			if (rep != 0) {
				return rep;
			}
		}

		return 0;
	}

	/**
	 * @param list
	 *            the List of lifts recorded for the given lift ID.
	 * @return returns the most recently recorded One Rep value.
	 */
	private int getLatestRepOne(List<Lift> list) {
		for (int i = list.size() - 1; i >= 0; i--) {
			int rep = list.get(i).getRepOne();

			if (rep != 0) {
				return rep;
			}
		}

		return 0;
	}

	/**
	 * @param list
	 *            the List of lifts recorded for the given lift ID.
	 * @return returns the most recently recorded Three Rep value.
	 */
	private int getLatestRepThree(List<Lift> list) {
		for (int i = list.size() - 1; i >= 0; i--) {
			int rep = list.get(i).getRepThree();

			if (rep != 0) {
				return rep;
			}
		}

		return 0;
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == REQUEST_CODE_DETAIL) {
			if (data != null) {
				Bundle extras = data.getExtras();
				int position = extras.getInt("LiftId");

				// setting the dates for the new current lifts just recorded.
				mLifts.get(position).setDateOne(
						new Date(extras.getLong("DateOne")));
				mLifts.get(position).setDateThree(
						new Date(extras.getLong("DateThree")));
				mLifts.get(position).setDateFive(
						new Date(extras.getLong("DateFive")));
				mLifts.get(position).setRepOne(extras.getInt("RepOne"));
				mLifts.get(position).setRepThree(extras.getInt("RepThree"));
				mLifts.get(position).setRepFive(extras.getInt("RepFive"));

				// accessing the content of the list of lifts
				View view = getListView().getChildAt(
						position - getListView().getFirstVisiblePosition());

				// setting the newly updated Rep values, if any.
				TextView txtRepOne = (TextView) view
						.findViewById(R.id.lifts_txt_recordone);
				txtRepOne.setText(Integer.toString(mLifts.get(position)
						.getRepOne()));

				TextView txtRepThree = (TextView) view
						.findViewById(R.id.lifts_txt_recordthree);
				txtRepThree.setText(Integer.toString(mLifts.get(position)
						.getRepThree()));

				TextView txtRepFive = (TextView) view
						.findViewById(R.id.lifts_txt_recordfive);
				txtRepFive.setText(Integer.toString(mLifts.get(position)
						.getRepFive()));

			}
		}
	}

	/**
	 * TODO: Need documentation.
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lifts);

		// Initializes local variables
		String[] names = getResources().getStringArray(R.array.lifts);
		String[] setups = getResources().getStringArray(R.array.lifts_setup);
		String[] executions = getResources().getStringArray(
				R.array.lifts_descriptions);
		String[] pictures = getResources()
				.getStringArray(R.array.lift_pictures);

		mLifts = new ArrayList<Lift>();
		mDatabase = new LocalDatabaseManager(this);

		// Creating a filling a List of Lift objects.
		for (int i = 0; i < names.length; i++) {
			List<Lift> list = mDatabase.getLiftByLiftId(i);
			Lift item = new Lift();

			item.setLiftId(i);
			item.setName(names[i]);
			item.setSetup(setups[i]);
			item.setExecution(executions[i]);
			item.setPicture(pictures[i]);
			item.setDateOne(getLatestDateOne(list));
			item.setDateThree(getLatestDateThree(list));
			item.setDateFive(getLatestDateFive(list));
			item.setRepOne(getLatestRepOne(list));
			item.setRepThree(getLatestRepThree(list));
			item.setRepFive(getLatestRepFive(list));

			mLifts.add(item);
		}

		mDatabase.close();

		// Creates an adapter using the list of girls.
		mAdapter = new LiftsAdapter(this, android.R.layout.simple_list_item_1,
				mLifts);
		setListAdapter(mAdapter);
	}
}